package com.qf.bootdaily.controller;


import com.github.pagehelper.PageInfo;
import com.qf.bootdaily.entity.Daily;
import com.qf.bootdaily.entity.User;
import com.qf.bootdaily.service.DailyService;
import com.qf.bootdaily.utils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
public class DailyController {
    @Autowired
    DailyService dailyService;
    //日报列表
    @GetMapping("/daily")
    public Response<PageInfo<Daily>> showAll(Integer pageNum, Integer pageSize, HttpSession session){
        User user = (User) session.getAttribute("user");
        PageInfo<Daily> pageInfo = dailyService.selectALl(pageNum, pageSize, user.getId());
        return Response.success(pageInfo);
    }


    @PostMapping("add")
    public Response<Daily> add(Daily daily,HttpSession session){
        System.out.println(daily);
        daily.setRegister(new Date());
        //获取用户id
        User user = (User) session.getAttribute("user");
        daily.setUserId(user.getId());
        System.out.println(daily);
        int i = dailyService.insertSelective(daily);
        if (i>0){
            return Response.success();
        }else {
            return Response.fail();
        }
    }

    //删除日报
    @GetMapping("delete")
    public Response<Daily> deleteByPrimaryKey(Integer id){
        int i = dailyService.deleteByPrimaryKey(id);
        return Response.success();
    }
}
